﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Tradex.Messages;
using Tradex.Trading;

namespace Tradex.Trading {
    
    /// <summary>
    /// This class represents an account. This is a dyhanicmally updating object.
    /// </summary>
    public sealed class Account : ComponentModel.DynamicObject {

        /// <summary>
        /// Constructor taking an account key.
        /// </summary>
        /// <param name="key"></param>
        public Account(AccountKey key) {
            Key = key;
        }

        /// <summary>
        /// Constructor taking the account key and returning the inpt action.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="inputAction"></param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1021")]
        public Account(AccountKey key, out Action<Message> inputAction) : base (out inputAction) {
            Key = key;
        }

        /// <inheritdoc />
        public override void ProcessMessage(Message message) {
            if (message == null) {
                return;
            }
            switch (message.Code) {
                case MessageCode.AccountUpdate:
                    AccountUpdateMessage accountUpdate = (AccountUpdateMessage) message;
                    if (accountUpdate.Info != null) {
                        if (accountUpdate.Info != Info) {
                            Info = accountUpdate.Info;
                        }
                    }
                    if (accountUpdate.Update != null) {
                        if (accountUpdate.Update != Update) {
                            Update = accountUpdate.Update;
                        }
                    }
                    break;
            }
        }

        /// <summary>
        /// This is the Key that identifies the account.
        /// </summary>
        public AccountKey Key { get; private set; }

        /// <summary>
        /// Gets the ConnectionKey
        /// </summary>
        public ConnectionKey ConnectionKey {
            get {
                lock (this) {
                    return _ConnectionKey;
                }
            }
            set {
                lock (this) {
                    OnPropertyChanged("ConnectionKey", ref _ConnectionKey, value);
                }
            }
        }
        ConnectionKey _ConnectionKey;

        /// <summary>
        /// Gets the AccountInfo
        /// </summary>
        public AccountInfo Info {
            get {
                lock (this) {
                    return _AccountInfo;
                }
            }
            private set {
                lock (this) {
                    OnPropertyChanged("Info", ref _AccountInfo, value);
                }
            }
        }
        AccountInfo _AccountInfo;

        /// <summary>
        /// Gets the AccountUpdate
        /// </summary>
        public AccountUpdate Update {
            get {
                lock (this) {
                    return _AccountUpdate;
                }
            }
            private set {
                lock (this) {
                    OnPropertyChanged("Update", ref _AccountUpdate, value);
                }
            }
        }
        AccountUpdate _AccountUpdate;

    }

}
